package com.wheat.vipcard.bean;

import java.util.ArrayList;
import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.criterion.Example;

import com.wheat.vipcard.utils.PageResultSet;
import com.wheat.vipcard.utils.PageUtil;

/**
 * A data access object (DAO) providing persistence and search support for Card
 * entities. Transaction control of the save(), update() and delete() operations
 * can directly support Spring container-managed transactions or they can be
 * augmented to handle user-managed Spring transactions. Each of these methods
 * provides additional information for how to configure it for the desired type
 * of transaction control.
 * 
 * @see com.wheat.vipcard.bean.Card
 * @author MyEclipse Persistence Tools
 */

public class CardDAO extends BaseHibernateDAO {
	private static final Log log = LogFactory.getLog(CardDAO.class);
	// property constants
	public static final String CARD_LOGO = "cardLogo";
	public static final String CARD_NAME = "cardName";
	public static final String CARD_TYPE = "cardType";
	public static final String CARD_SUMMARY = "cardSummary";
	public static final String CARD_SALE_CODE = "cardSaleCode";
	public static final String CARD_ADDRESS = "cardAddress";
	public static final String CARD_TASTE = "cardTaste";
	public static final String CARD_SUMMARY_CONTENT = "cardSummaryContent";
	public static final String CARD_CONTENT = "cardContent";
	public static final String CARD_VALIDITY_TIME = "cardValidityTime";
	public static final String CARD_DOWN_LOAD_COUNT = "cardDownLoadCount";
	public static final String CARD_SPECIAL_TIPS = "cardSpecialTips";
	public static final String CARD_SPECIAL_TIPS_CONTENT = "cardSpecialTipsContent";
	public static final String CARD_ADDITIONAL_CONTENT1 = "cardAdditionalContent1";
	public static final String CARD_ADDITIONAL_CONTENT2 = "cardAdditionalContent2";
	public static final String CARD_CLASS = "cardClass";
	public static final String BS_ID = "bsId";
	public static final String CARD_ZONE = "cardZone";
	public static final String GREAD = "gread";

	public void save(Card transientInstance) {
		log.debug("saving Card instance");
		try {
			getSession().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	public void delete(Card persistentInstance) {
		log.debug("deleting Card instance");
		try {
			getSession().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public Card findById(java.lang.Integer id) {
		log.debug("getting Card instance with id: " + id);
		try {
			Card instance = (Card) getSession().get("com.wheat.vipcard.bean.Card", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List findByExample(Card instance) {
		log.debug("finding Card instance by example");
		try {
			List results = getSession().createCriteria("com.wheat.vipcard.bean.Card").add(Example.create(instance))
					.list();
			log.debug("find by example successful, result size: " + results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding Card instance with property: " + propertyName + ", value: " + value);
		try {
			String queryString = "from Card as model where model." + propertyName + "= ?";
			Query queryObject = getSession().createQuery(queryString);
			queryObject.setParameter(0, value);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List findByCardLogo(Object cardLogo) {
		return findByProperty(CARD_LOGO, cardLogo);
	}

	public List findByCardName(Object cardName) {
		return findByProperty(CARD_NAME, cardName);
	}

	public List findByCardType(Object cardType) {
		return findByProperty(CARD_TYPE, cardType);
	}

	public List findByCardSummary(Object cardSummary) {
		return findByProperty(CARD_SUMMARY, cardSummary);
	}

	public List findByCardSaleCode(Object cardSaleCode) {
		return findByProperty(CARD_SALE_CODE, cardSaleCode);
	}

	public List findByCardAddress(Object cardAddress) {
		return findByProperty(CARD_ADDRESS, cardAddress);
	}

	public List findByCardTaste(Object cardTaste) {
		return findByProperty(CARD_TASTE, cardTaste);
	}

	public List findByCardSummaryContent(Object cardSummaryContent) {
		return findByProperty(CARD_SUMMARY_CONTENT, cardSummaryContent);
	}

	public List findByCardContent(Object cardContent) {
		return findByProperty(CARD_CONTENT, cardContent);
	}

	public List findByCardValidityTime(Object cardValidityTime) {
		return findByProperty(CARD_VALIDITY_TIME, cardValidityTime);
	}

	public List findByCardDownLoadCount(Object cardDownLoadCount) {
		return findByProperty(CARD_DOWN_LOAD_COUNT, cardDownLoadCount);
	}

	public List findByCardSpecialTips(Object cardSpecialTips) {
		return findByProperty(CARD_SPECIAL_TIPS, cardSpecialTips);
	}

	public List findByCardSpecialTipsContent(Object cardSpecialTipsContent) {
		return findByProperty(CARD_SPECIAL_TIPS_CONTENT, cardSpecialTipsContent);
	}

	public List findByCardAdditionalContent1(Object cardAdditionalContent1) {
		return findByProperty(CARD_ADDITIONAL_CONTENT1, cardAdditionalContent1);
	}

	public List findByCardAdditionalContent2(Object cardAdditionalContent2) {
		return findByProperty(CARD_ADDITIONAL_CONTENT2, cardAdditionalContent2);
	}

	public List findByCardClass(Object cardClass) {
		return findByProperty(CARD_CLASS, cardClass);
	}

	public List findByBsId(Object bsId) {
		return findByProperty(BS_ID, bsId);
	}

	public List findByCardZone(Object cardZone) {
		return findByProperty(CARD_ZONE, cardZone);
	}

	public List findByGread(Object gread) {
		return findByProperty(GREAD, gread);
	}

	public List findAll() {
		log.debug("finding all Card instances");
		try {
			String queryString = "from Card";
			Query queryObject = getSession().createQuery(queryString);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public Card merge(Card detachedInstance) {
		log.debug("merging Card instance");
		try {
			Card result = (Card) getSession().merge(detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(Card instance) {
		log.debug("attaching dirty Card instance");
		try {
			getSession().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(Card instance) {
		log.debug("attaching clean Card instance");
		try {
			getSession().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	/**
	 * 
	 * @param card
	 * @param pageNo
	 * @param pageSize
	 * @return
	 */
	public PageResultSet<Card> findCardByPage(Card card, int pageNo, int pageSize) {
		String sqlString = bulidQuerySQL(card);
		int totalRow = queryCount(sqlString);
		PageUtil pages = new PageUtil(totalRow, pageSize, pageNo);
		List<Card> list = new ArrayList<Card>();
		List<Object[]> objList = queryList(sqlString, pageNo, pageSize);
		for (Object[] b : objList) {
			Card c = new Card();
			c.setId(b[0] == null ? 0 : Integer.parseInt(b[0].toString()));
			c.setCardLogo(b[1] == null ? "" : b[1].toString());
			c.setCardName(b[2] == null ? "" : b[2].toString());
			c.setCardType(b[3] == null ? 0 : Integer.parseInt(b[3].toString()));
			c.setCardSummary(b[4] == null ? "" : b[4].toString());
			c.setCardSaleCode(b[5] == null ? 0d : Double.parseDouble(b[5].toString()));
			c.setCardAddress(b[6] == null ? "" : b[6].toString());
			c.setCardTaste(b[7] == null ? "" : b[7].toString());
			c.setCardSummaryContent(b[8] == null ? "" : b[8].toString());
			c.setCardContent(b[9] == null ? "" : b[9].toString());
			c.setCardValidityTime(b[10] == null ? "" : b[10].toString());
			c.setCardDownLoadCount(b[11] == null ? 0 : Integer.parseInt(b[11].toString()));
			c.setCardSpecialTips(b[12] == null ? "" : b[12].toString());
			c.setCardSpecialTipsContent(b[13] == null ? "" : b[13].toString());
			c.setCardAdditionalContent1(b[14] == null ? "" : b[14].toString());
			c.setCardAdditionalContent2(b[15] == null ? "" : b[15].toString());
			c.setCardClass(b[16] == null ? 0 : Integer.parseInt(b[16].toString()));
			c.setBsId(b[17] == null ? 0 : Integer.parseInt(b[17].toString()));
			c.setCardZone(b[18] == null ? 0 : Integer.parseInt(b[18].toString()));
			c.setGread(b[19] == null ? 0d : Double.parseDouble(b[19].toString()));

			list.add(c);
		}
		PageResultSet<Card> page = new PageResultSet<Card>();
		page.setList(list);
		page.setPage(pages);
		return page;
	}

	/**
	 * 构建查询sql
	 * 
	 * @param card
	 * @return
	 */
	public String bulidQuerySQL(Card card) {
		String querySQL = "select * from card c";
		if (card != null) {
			if (card.getCardType() != 0) {
				querySQL += " and c.cardType='" + card.getCardType() + "'";
			}
			if (card.getCardClass() != 0) {
				querySQL += " and c.cardClass='" + card.getCardClass() + "'";
			}
		}
		return querySQL;
	}
}